package com.ruoyi.caigou.controller;

import com.ruoyi.caigou.service.IBuyBackReqService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.caigou.BuyBackReq;
import com.ruoyi.system.api.domain.caigou.BuyBackReqDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 采购退货申请单Controller
 *
 * @author wl
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/buyBackReq")
@Slf4j
public class BuyBackReqController extends BaseController {
    @Autowired
    private IBuyBackReqService buyBackReqService;

    /**
     * 查询采购退货申请单列表
     */
    @RequiresPermissions("caigou:buyBackReq:list")
    @GetMapping("/list")
    public TableDataInfo list(BuyBackReq buyBackReq) {
        log.info("BuyBackReqController list buyBackReq:{}", buyBackReq);
        List<BuyBackReq> list = buyBackReqService.selectBuyBackReqList(buyBackReq);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("BuyBackReqController list tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * @return com.ruoyi.common.core.domain.R<java.util.List < com.ruoyi.system.api.domain.caigou.BuyBackReqDetail>>
     * @description 根据采购退货单id获取详情
     * @date 2024/11/5 下午10:37
     * @Param buyBackReqId:
     **/
    //    @RequiresPermissions("caigou:buyBackReq:list")
    @GetMapping("/buyBackReqDetail/list/{buyBackReqId}")
    public AjaxResult getBuyBackReqDetailListByBuyBackReqId(@PathVariable("buyBackReqId") Integer buyBackReqId) {
        log.info("BuyBackReqController getBuyBackReqDetailList buyBackReqId:{}", buyBackReqId);
        List<BuyBackReqDetail> list = buyBackReqService.selectBuyBackReqDetailListByBuyBackReqId(buyBackReqId);
        AjaxResult ajaxResult = success(list);
        log.info("BuyBackReqController getBuyBackReqDetailList ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 导出采购退货申请单列表
     */
    @RequiresPermissions("caigou:buyBackReq:export")
    @Log(title = "采购退货申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyBackReq buyBackReq) {
        List<BuyBackReq> list = buyBackReqService.selectBuyBackReqList(buyBackReq);
        ExcelUtil<BuyBackReq> util = new ExcelUtil<BuyBackReq>(BuyBackReq.class);
        util.exportExcel(response, list, "采购退货申请单数据");
    }

    /**
     * 获取采购退货申请单详细信息
     */
    @RequiresPermissions("caigou:buyBackReq:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("BuyBackReqController getInfo id:{}", id);
        AjaxResult ajaxResult = success(buyBackReqService.selectBuyBackReqById(id));
        log.info("BuyBackReqController getInfo ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    @GetMapping("/info/code")
    public AjaxResult getInfoByCode(@RequestParam("code") String code) {
        log.info("BuyBackReqController getInfoByCode code:{}", code);
        BuyBackReq buyBackReq = buyBackReqService.selectBuyBackReqByCode(code);
        log.info("BuyBackReqController getInfoByCode buyBackReq:{}", buyBackReq);
        return success(buyBackReq);
    }

    /**
     * 新增采购退货申请单
     */
//    @RequiresPermissions("caigou:buyBackReq:add")
    @Log(title = "采购退货申请单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyBackReq buyBackReq) {
        log.info("BuyBackReqController add buyBackReq:{}", buyBackReq);
        AjaxResult ajaxResult = buyBackReqService.insertBuyBackReq(buyBackReq);
        log.info("BuyBackReqController add ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改采购退货申请单
     */
//    @RequiresPermissions("caigou:buyBackReq:edit")
    @Log(title = "采购退货申请单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyBackReq buyBackReq) {
        log.info("BuyBackReqController edit buyBackReq:{}", buyBackReq);
        AjaxResult ajaxResult = buyBackReqService.updateBuyBackReq(buyBackReq);
        log.info("BuyBackReqController edit ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 删除采购退货申请单
     */
    @RequiresPermissions("caigou:buyBackReq:remove")
    @Log(title = "采购退货申请单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("BuyBackReqController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = buyBackReqService.deleteBuyBackReqByIds(ids);
        log.info("BuyBackReqController remove ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    @PutMapping("/updateStatusByCode")
    public AjaxResult updateStatusByCode(@RequestBody BuyBackReq buyBackReq) {
        log.info("BuyBackReqController updateStatusByCode buyBackReq:{}", buyBackReq);
        AjaxResult ajaxResult = buyBackReqService.updateStatusByCode(buyBackReq);
        log.info("BuyBackReqController updateStatusByCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

}
